﻿using System;
using Game.ExtensibleObjects;

namespace Game.Map.Extensions
{
    public static class SelectedExtensionHelper
    {
        public static MapSelectedExtension SelectedX(this IGameMap map)
        {
            return map.Extensions.Find<MapSelectedExtension>();
        }
    }

    public class MapSelectedExtension : IRuntimeExtension
    {
        public event EventHandler SelectedChanged;

        IMapField _Selected;

        public IMapField Selected
        {
            get { return _Selected; }
            set
            {
                if (_Selected != null)
                {
                    _Selected.SelectedX().Selected = false;
                }
                _Selected = value;
                DoSelectedChanged();
                if (_Selected != null)
                {
                    _Selected.SelectedX().Selected = true;
                }
            }
        }

        void DoSelectedChanged()
        {
            if (SelectedChanged != null)
            {
                var args = new EventArgs();
                SelectedChanged(this, args);
            }
        }

    }

    public class MapSelectedExtensionFactory : IRuntimeExtensionFactory
    {
        public IRuntimeExtension CreateExtension(IRuntimeExtensibleObject source)
        {
            return new MapSelectedExtension();
        }
    }
}
